﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DodgyBobStockControl;
using System.IO;

namespace SurehealthForms
{
    namespace DodgyBobStockControl
    {
        class Stock
        {
            static string[] StockArray;

            /// <summary>
            /// Initilises the database and loads into the StockArray
            /// </summary>
            /// <returns>true if it has loaded successfully</returns>
            public static bool InitialiseDatabase()
            {
                // Initialise Bob's System
                if (!File.Exists("dodgystock.dat"))
                {
                    StockControl.INITIALISE("SureHealthItems.txt");
                }
                else
                {
                    StockControl.INITIALISE();
                }
                StockControl.LOAD();


                StockArray = StockControl.GET_ITEMS();

               
                return true;
            }

            /// <summary>
            /// Updates the database
            /// </summary>
            /// <returns>true if its saved succesfully </returns>
            public static bool SaveDatabase()
            {
                StockControl.SAVE();
                return true;
            }

            /// <summary>
            /// Gives a user a copy of the StockList
            /// </summary>
            /// <returns>string array of stock items</returns>
            public static string[] StockList()
            {
                InitialiseDatabase();
                return StockArray;
            }


            /// <summary>
            /// Checks if it is a valid item
            /// </summary>
            /// <param name="itemName">Pass it a name of them item</param>
            /// <returns>true if it is in the database</returns>
            public static bool IsValidItem(string itemName)
            {
                string Output = StockControl.ASK("'" + itemName + "' cost please");
                string[] SplitOutput = Output.Split(new char[] { ' ' });
                if (SplitOutput[0] == "What")
                    return false;
                else
                    return true;
            }

            /// <summary>
            /// Gives a int for the amount of stock for a Item Name
            /// </summary>
            /// <param name="itemName">Pass it a name of them item</param>
            /// <returns>int of the stock or 0 if its not found</returns>
            public static int StockCheck(string itemName)
            {
                InitialiseDatabase();
                string Output = StockControl.ASK("'" + itemName + "' stockcheck please");
                string[] SplitOutput = Output.Split(new char[] { ' ' });
                if (SplitOutput[0] == "What")
                    return 0;
                if (SplitOutput[0] == "The")
                    return 0;
                else
                {
                    SaveDatabase();
                    return int.Parse(SplitOutput[2]);
                }

            }


            /// <summary>
            /// Removes all previous stock and puts the amount in if successful
            /// This also will add onto the date until it is in date 
            /// </summary>
            /// <param name="itemName">pass the Item Name</param>
            /// <param name="Amount">give a int of the amount of items you wish to add</param>
            /// <returns>true if it is successful </returns>
            public static bool RestockItem(string itemName, int Amount)
            {
                InitialiseDatabase();

                if (Amount >= 1)
                {
                    do
                    {
                        if(IsValidItem(itemName))
                        {
                            StockControl.DO("'" + itemName + "' restock " + Amount + " please");
                            SaveDatabase();
                        }
                        else
                            return false;
                    } while (!CheckInDate(itemName));
                    
                    return true;
                }
                else
                    return false;
                 
            }


            /// <summary>
            /// Reduces the amount of stock for the item name
            /// </summary>
            /// <param name="itemName">The item name you wish to reduce</param>
            /// <param name="amount">The amount you wish to use</param>
            /// <returns>True if successful </returns>
            public static bool UseStock(string itemName, int amount)
            {
                InitialiseDatabase();
                string Output = "";

                if (StockCheck(itemName) >= amount)
                    Output = StockControl.DO("'" + itemName + "' consume " + amount + " please");
                else
                    return false;
                string[] SplitOutput = Output.Split(new char[] { ' ' });
                if (SplitOutput[0] == "What")
                    return false;
                else
                {
                    SaveDatabase();
                    return true;
                }

            }

            /// <summary>
            /// Returns a number of a single item into the system again
            /// </summary>
            /// <param name="itemName">The name of the item you are returning</param>
            /// <param name="returnAmount">The amount of the single item your returning</param>
            /// <returns>True if successful </returns>
            public static bool returnItem(string itemName, int returnAmount)
            {
                InitialiseDatabase();
                if (returnAmount >= 1)
                {
                    if (IsValidItem(itemName))
                    {
                        StockControl.DO("'" + itemName + "' return " + returnAmount + " please");
                        SaveDatabase();
                        return true;
                    }
                    else
                        return false;
                }
                return false; 
            }

            /// <summary>
            /// Used for querying the cost of a item name
            /// </summary>
            /// <param name="itemName">Pass the item name you wish to check</param>
            /// <returns>The price of the item in a decimal</returns>
            public static decimal StockQueryCost(string itemName)
            {
                InitialiseDatabase();
                if(IsValidItem(itemName))
                {
                    string Output = StockControl.ASK("'" + itemName + "' cost please");
                    string[] SplitOutput2 = Output.Split(new char[] { '£' });
                    return decimal.Parse(SplitOutput2[1]);
                }
                else
                    return 0;
            }

            /// <summary>
            /// Querys the database for the Expiry Date of a Item Name
            /// </summary>
            /// <param name="itemName">The Item Name you wish to check</param>
            /// <returns>A Datetime of the Expiry Date of the item entered</returns>
            public static DateTime StockQueryExpiry(string itemName)
            {
                InitialiseDatabase();
                if (IsValidItem(itemName))
                {
                    string Output = StockControl.ASK("'" + itemName + "' expiry please");
                    string[] SplitOutput = Output.Split(new char[] { ' ' });
                    DateTime Date = DateTime.Parse(SplitOutput[3]);
                    string SDate = Date.ToString("d");
                    string[] DateArray = SDate.Split(new char[] { '/' });
                    string yearS = DateArray[2];
                    char[] yearC = yearS.ToCharArray();
                    int YearP1 = int.Parse(yearC[0].ToString() + yearC[1].ToString());
                    int YearP2 = int.Parse(yearC[2].ToString() + yearC[3].ToString());

                    if (YearP2 > 50)
                        YearP1 = 19;

                    string year = YearP1.ToString() + YearP2.ToString();
                    return new DateTime(int.Parse(year), int.Parse(DateArray[1]), int.Parse(DateArray[0]));
                }
                else
                    return new DateTime(1337, 10, 1);

            }

            /// <summary>
            /// Returns true/false wether a item name is in date or not
            /// </summary>
            /// <param name="itemname">The Item Name of which you wish to check</param>
            /// <returns>bool of wether in date or not</returns>
            public static bool CheckInDate(string itemName)
            {
                if (StockQueryExpiry(itemName) < DateTime.Today)
                    return false;
                else
                    return true;
            }

        }
    }
}
